package com.rabc.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.base.BaseController;
import com.rabc.web.entity.SysUser;
import com.rabc.web.service.SysUserService;
import com.utils.EncryptionUtils;
import com.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.*;

@Slf4j
@RestController
@Api(tags = "用户管理")
@RequestMapping("/user")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;


    @GetMapping("/list/v1")
   public  R list(SysUser sysUser){
        return R.success(sysUserService.selectUserList(sysUser));
    }


    @ApiOperation(value = "获取数据")
    @GetMapping("/update")
    public R edit(@RequestBody SysUser sysUser) {
        boolean byId = sysUserService.updateById(sysUser);
        if (byId) {
            return R.success("SysUser修改成功");

        } else {
            return R.error("SysUser修改失败");
        }
    }

    @GetMapping("/info/{userId}")
    public R getInfo(@PathVariable("userId") String userId) {
        List<SysUser> list = sysUserService.list(new QueryWrapper<SysUser>().eq("user_id", userId));
        return R.success(list);
    }

    @GetMapping("/remove/{userId}")
    public R remove(@PathVariable("userId") String userId) {
      boolean i  = sysUserService.removeById(userId);
      if (i){

          return R.success();
      }else {
          return R.success();
      }
    }
    @PostMapping("/add")
    public R add(@RequestBody SysUser user) throws NoSuchAlgorithmException {
        String password = user.getPassword();
        String pwdMd5 = EncryptionUtils.encryptBCrypt(password);
        user.setPassword(pwdMd5);
        boolean save = sysUserService.save(user);
        if (save) {
            return R.success();
        }
        return R.error();
    }
    @GetMapping("/feature")
    public R feature() {
        log.info("======================chapterAPi ==================");
        return R.success(generateData());
    }

    @GetMapping("/chapter")
    public R chapter() {
        return R.success(generateDataList());
    }

    @GetMapping("/list")
    public R list() {
        return R.success(generateResponseData());
    }

    @GetMapping("/detail/{id}")
    public R detail(@PathVariable(name = "id") String id) {
        return R.success(constructData());
    }

    @GetMapping("/role/{id}")
    public R role(@PathVariable(name = "id") String id) {
        return buildResponse();
    }

    @PostMapping("/update-role/{id}")
    public R update(@PathVariable(name = "id") String id) {
        return R.success();

    }



    public static Map<String, Object> generateResponseData() {
        Map<String, Object> responseData = new HashMap<>();

        List<Map<String, Object>> list = new ArrayList<>();

        // 构造第一个用户数据
        list.add(createUser(
                "1", "超级管理员", "612710a0ec87aa543c9c341d", "0",
                "1433088000000", "super-admin", "188xxxx0001",
                "https://m.imooc.com/static/wap/static/common/img/logo-small@2x.png"
        ));

        // 构造第二个用户数据
        list.add(createUser(
                "2", "管理员", "612710a0ec87aa543c9c341e", "1",
                "1559318400000", "admin", "188xxxx0002",
                "https://img4.sycdn.imooc.com/61110c2b0001152907400741-140-140.jpg"
        ));

        responseData.put("list", list);
        responseData.put("total", 7);
        responseData.put("page", "1");
        responseData.put("size", "2");

        return responseData;
    }

    private static Map<String, Object> createUser(String roleId, String roleTitle, String _id, String id,
                                                  String openTime, String username, String mobile, String avatar) {
        Map<String, Object> user = new HashMap<>();

        List<Map<String, Object>> roleList = new ArrayList<>();
        Map<String, Object> role = new HashMap<>();
        role.put("id", roleId);
        role.put("title", roleTitle);
        roleList.add(role);

        user.put("role", roleList);
        user.put("_id", _id);
        user.put("id", id);
        user.put("openTime", openTime);
        user.put("username", username);
        user.put("mobile", mobile);
        user.put("avatar", avatar);

        return user;
    }


    public static List<Map<String, Object>> generateDataList() {
        List<Map<String, Object>> dataList = new ArrayList<>();

        // 构造每个条目的数据
        dataList.add(createItem("课程导读", "第一章", 1));
        dataList.add(createItem("标准化大厂编程规范解决方案之ESLint + Git Hooks", "第二章", 2));
        dataList.add(createItem("项目架构解决方案之搭建登录基础架构", "第三章", 3));
        dataList.add(createItem("项目架构解决方案之搭建Layout基础架构", "第四章", 4));
        dataList.add(createItem("后台项目前端综合解决方案之 通用功能开发", "第五章", 5));
        dataList.add(createItem("vue3 + element plus 综合实现解决方案之个人中心页面", "第六章", 6));
        dataList.add(createItem("excel 、zip 与前端结合解决方案之用户管理页面实现", "第七章", 7));
        dataList.add(createItem("权限控制解决方案之角色、权限页面渲染实现项目的权限控制功能", "第八章", 8));
        dataList.add(createItem("列表排序解决方案之实现热门文章排名", "第九章", 9));
        dataList.add(createItem("文章编辑解决方案之实现创建文章功能", "第十章", 10));
        dataList.add(createItem("错误综合处理方案之错误页面的统一处理", "第十一章", 11));
        dataList.add(createItem("项目发布解决方案之构建与发布", "第十二章", 12));
        dataList.add(createItem("课程总结", "第十三章", 13));

        return dataList;
    }

    private static Map<String, Object> createItem(String content, String timestamp, int id) {
        Map<String, Object> item = new HashMap<>();
        item.put("content", content);
        item.put("timestamp", timestamp);
        item.put("id", id);
        return item;
    }


    private static Map<String, Object> createItem(String title, int percentage, String content) {
        Map<String, Object> item = new HashMap<>();
        item.put("title", title);
        item.put("percentage", percentage);
        item.put("content", content);
        return item;
    }


    public static List<Map<String, Object>> generateData() {
        Map<String, Object> response = new HashMap<>();


        List<Map<String, Object>> dataList = new ArrayList<>();

        // 构造每个条目的数据
        dataList.add(createItem("Vue3 + 全家桶",
                100,
                "项目基于最新的<a target=\"_blank\" href=\"https://v3.cn.vuejs.org/guide/introduction.html\">vue3</a>全家桶进行开发，全面使用最新的的<a target=\"_blank\" href=\"https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md\">RFC script setup</a>语法标准，为你带来不一样的 vue3 开发体验"));

        dataList.add(createItem("Element-Plus",
                100,
                "<a target=\"_blank\" href=\"https://element-plus.org/#/zh-CN\">Element Plus</a>，一套为开发者、设计师和产品经理准备的基于 Vue 3.0 的桌面端组件库。是 Element UI 的官方 vue 3 兼容版本"));

        dataList.add(createItem("vue-element-admin",
                100,
                "<a target=\"_blank\" href=\"https://panjiachen.github.io/vue-element-admin-site/zh/guide/\">vue-element-admin</a> 是一个后台前端解决方案，它基于 vue 和 element-ui实现，并有用 70K 的 <a target=\"_blank\" href=\"https://github.com/PanJiaChen/vue-element-admin\">star</a>。<br />本项目使用最新 vue 技术对其进行了重构，并在其现有功能上进行了扩展，致力于为大家带来更加符合 <b>现代后台开发的前端技术解决方案</b>"));

        dataList.add(createItem("ESLint + Git Hooks",
                100,
                "ESLint 对应编码规范，Git Hooks 对应 Git 规范。<br /> 想要开发出大厂标准的企业级项目，需要先从规范做起！"));

        dataList.add(createItem("架构设计",
                100,
                "阿里前端 P7 岗被称为 《技术专家》，也有人喜欢叫他们为 “前端架构师”，既然被称为 “架构师” ，那么架构设计当然是不可缺少的一环咯"));

        dataList.add(createItem("权限验证（页面权限、功能权限、动态权限、权限配置）",
                100,
                "全面的后台权限验证系统，实现了 <b>页面权限、功能权限、动态权限、权限配置</b> 等全方位的权限功能"));

        dataList.add(createItem("功能引导",
                100,
                "对用户的功能引导"));

        dataList.add(createItem("可配置 + 收缩侧边栏",
                100,
                "根据路由动态生成的 Menu 菜单，实现真正的 <b>配置 Menu</b>"));

        dataList.add(createItem("动态面包屑",
                100,
                "动态生成的面包屑数据"));

        dataList.add(createItem("Tags View",
                100,
                "便签快捷导航功能"));

        dataList.add(createItem("svg sprite 图标",
                100,
                "自定义的 svg Icon 配合 el Icon 自动分配导入"));

        dataList.add(createItem("国际化",
                100,
                "多语言的国际化功能实现"));

        dataList.add(createItem("动态换肤",
                100,
                "主题更换功能，提供缓存实现"));

        dataList.add(createItem("页面搜索",
                100,
                "根据路由配置自动化生成的页面搜索数据，无需手动配置"));

        dataList.add(createItem("excel 导出、导入",
                100,
                "excel 表格的导入、导出实现"));

        dataList.add(createItem("zip 导入",
                100,
                "页面数据导出为 zip"));

        dataList.add(createItem("富文本 + MarkDown 编辑器",
                100,
                "常用的编辑器功能，包含 富文本编辑器 && MarkDown 编辑器"));

        dataList.add(createItem("动态表格 + 拖拽表格 + 内联编辑表格",
                100,
                "表格列表的常见操作，多用于包含表格项的展示功能中。包括：动态表格 && 拖拽表格 && 内联编辑表格"));

        dataList.add(createItem("统一错误处理",
                100,
                "页面级的错误处理由 vue-router 统一处理。包含 401 和 404 的错误处理"));

        dataList.add(createItem("发布",
                100,
                "构建与发布"));

        dataList.add(createItem("其他",
                100,
                "除了以上功能之外，我们还实现了很多其他的功能，比如：CDN、动态的环境变量配置、懒加载、跨域等等"));

        response.put("data", dataList);

        return dataList;
    }

    private Map<String, Object> constructData() {
        Map<String, Object> data = new HashMap<>();

        // 构造 role
        Map<String, String> role = new HashMap<>();
        role.put("id", "1");
        role.put("title", "超级管理员");
        data.put("role", Arrays.asList(role));

        // 构造 remark
        data.put("remark", Arrays.asList("超级管理员", "BOSS"));

        // 构造 experience
        Map<String, String> exp1 = new HashMap<>();
        exp1.put("startTime", "1538323200000");
        exp1.put("endTime", "1551369600000");
        exp1.put("title", "慕课网");
        exp1.put("desc", "混合开发京东商城");

        Map<String, String> exp2 = new HashMap<>();
        exp2.put("startTime", "1614528000000");
        exp2.put("endTime", "1625068800000");
        exp2.put("title", "慕课网");
        exp2.put("desc", "uni-app 开发企业级小程序");

        data.put("experience", Arrays.asList(exp1, exp2));

        // 设置其他字段
        data.put("_id", "612710a0ec87aa543c9c341d");
        data.put("id", "0");
        data.put("openTime", "1433088000000");
        data.put("username", "super-admin");
        data.put("title", "超级管理员");
        data.put("mobile", "188xxxx0001");
        data.put("avatar", "https://m.imooc.com/static/wap/static/common/img/logo-small@2x.png");
        data.put("gender", "男");
        data.put("nationality", "汉");
        data.put("address", "北京市朝阳区xx大道 11xx0 号 3 层");
        data.put("major", "在线职业教育平台");
        data.put("glory", "国内领先的线上 IT 教育品牌");

        return data;
    }

    public static R buildResponse() {


        Map<String, Object> data = new HashMap<>();
        Map<String, Object> role = new HashMap<>();
        role.put("id", "2");
        role.put("title", "管理员");

        data.put("role", new Object[]{role});
        data.put("_id", "6124eacc0dafe231304546be");

        return R.success(data);
    }


    public static R buildalllistResponse() {
        // 创建两个用户的role列表
        List<Map<String, Object>> role1 = new ArrayList<>();
        List<Map<String, Object>> role2 = new ArrayList<>();

        // 创建第一个用户的role
        Map<String, Object> role1Entry = new HashMap<>();
        role1Entry.put("id", "1");
        role1Entry.put("title", "超级管理员");
        role1.add(role1Entry);

        // 创建第二个用户的role
        Map<String, Object> role2Entry = new HashMap<>();
        role2Entry.put("id", "2");
        role2Entry.put("title", "管理员");
        role2.add(role2Entry);

        // 创建第一个用户
        Map<String, Object> user1 = new HashMap<>();
        user1.put("role", role1.toArray());
        user1.put("_id", "612710a0ec87aa543c9c341d");
        user1.put("id", "0");
        user1.put("openTime", "1433088000000");
        user1.put("username", "super-admin");
        user1.put("mobile", "188xxxx0001");
        user1.put("avatar", "https://m.imooc.com/static/wap/static/common/img/logo-small@2x.png");

        // 创建第二个用户
        Map<String, Object> user2 = new HashMap<>();
        user2.put("role", role2.toArray());
        user2.put("_id", "612710a0ec87aa543c9c341e");
        user2.put("id", "1");
        user2.put("username", "admin");
        user2.put("openTime", "1559318400000");
        user2.put("mobile", "188xxxx0002");
        user2.put("avatar", "https://img4.sycdn.imooc.com/61110c2b0001152907400741-140-140.jpg");

        // 创建用户列表
        List<Map<String, Object>> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user2);

        // 创建data
        Map<String, Object> data = new HashMap<>();
        data.put("list", userList.toArray());
        data.put("total", 7);
        data.put("page", "1");
        data.put("size", "2");

        return R.success(data);
    }

}
